/**
* 
* programme launch main window
* Copyright (c) 2009 Ecole Centrale de Nantes IRCCyN  
* Classe: programme.java 
* see <a href="{@docRoot}/documentation/trace.html">Trace for more information</a>
* 
* @author: Urunuela Richard
* @version 3.2
*/

package programme;

//import generator.generator;
import info.monitorenter.gui.chart.Chart2D;
import info.monitorenter.gui.chart.ITrace2D;
import info.monitorenter.gui.chart.ZoomableChart;
import info.monitorenter.gui.chart.traces.Trace2DSimple;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.xml.parsers.ParserConfigurationException;

import log.DVSTrace;
import log.TaskTrace;
import log.messageTrace;
import log.penalityTrace;
import log.powerTrace;

import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.GraphModel;


import org.jgraph.graph.GraphModel;
import org.xml.sax.SAXException;







//import storm.TasksListeManager.MNode;
import programme.GUI.*;
import programme.*;
import report.classSonde;
import report.report;
import storm.ISimulationKernel;
import storm.Data.Data;
import storm.Processors.Processor;
import storm.Tasks.Task;
/**
 * 
 * @author richard
 *
 */
public class programme {
	storm.SimulationKernel MonKL;

	public ISimulationKernel getMonKL() {
		return MonKL;
	}

	private String Filename;
	public GUIProgramme MyInterface;

	ArrayList listegraphe;
	boolean lightExec=false;
	
	  public boolean isLightExec() {
		return lightExec;
	}
/**
 * A faire
 * @param lightExec
 */
	public void setLightExec(boolean lightExec) {
		this.lightExec = lightExec;
	}
/**
 * :)
 * @param args
 */
	public static void main(String[] args)
	    
	  {

		programme P= new programme();
		   P.MyInterface =new GUIProgramme();
		   //P.MyInterface.setBackground(Color.white);
		   P.MyInterface.Controle.setprogramme(P);
		   String cmde = "";
		   for (int i=0; i <args.length ; i ++) {
			 if (args[i]!=null)  cmde = cmde  +  "  " + args[i];
			
		   }
	
		   if (cmde.length()>1){
			  
			   P.MyInterface.Controle.execCommande(cmde);
		   }
	  }
/**
 * 
 * @param N file name file used 
 */
	public void addFileNameSimu(String N) {
		// TODO Auto-generated method stub
		this.Filename =N;
		
	}
/**
 * Create and run simulation (use filename)
 * @param console 
 * @throws SAXException
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws RuntimeException
 * @throws Throwable
 * @throws NoSuchMethodException
 * @throws InstantiationException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
	public void createSimu(JConsole console) throws SAXException, IOException, ParserConfigurationException, RuntimeException, Throwable, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
	
		listegraphe = new ArrayList();
		   MonKL = new storm.SimulationKernel(console);
		   
		   stormIO.SimulatorLoader xmlFile = new stormIO.SimulatorLoader();
			   try {
				xmlFile.parse(this.Filename,System.out);
			} catch (SAXException e) {
				e.printStackTrace();
				throw (e);
			} catch (IOException e) {
				e.printStackTrace();
				throw (e);
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
				throw (e);
			} catch (RuntimeException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
				throw (e);
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
				throw (e);
			} catch (InstantiationException e) {
				e.printStackTrace();
				throw (e);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
				throw (e);
			} catch (InvocationTargetException e) {
				e.printStackTrace();
				throw (e);
			}  
		MonKL.setDuration(xmlFile.duration);
		MonKL.setPrecision(xmlFile.precision);
		MonKL.setAllTrace(xmlFile.getAllTrace());
		MonKL.addEntity(xmlFile.Sched);
		
		
	/**
	 * DPM
	 */
		if (xmlFile.DPM != null){
			MonKL.addEntity(xmlFile.DPM);
			
		}

		for (int j=0; j < xmlFile.CPUS.size(); j++){
			Processor P=(Processor) xmlFile.CPUS.get(j);
			MonKL.addEntity(P);
		}
		for (int j=0; j < xmlFile.listTask.size(); j++){
			Task T=(Task) xmlFile.listTask.get(j);
			MonKL.addEntity(T);
		}
		for (int j=0; j < xmlFile.listData.size(); j++){
			Data D=(Data) xmlFile.listData.get(j);
			
			MonKL.addEntity(D);
		}
		if (xmlFile.getReport() != null ) {
			MonKL.setReport (xmlFile.getReport());
	  		
	  	}
		if (xmlFile.Observer != null){
			MonKL.addEntity(xmlFile.Observer);
			
		}
	  	MonKL.Launch(MyInterface);
	  	
		
	}
/**
 * Plotall gantt graph
 */
	public void showGrapTemp() {
		for (int j=0 ; j < MonKL.Logs.getNbreCpu();j++) {
			InternalFrame f = new InternalFrame("InnerFrame ");
	
			  f.setBounds(100, 50+(j*10),610, 160);
			 f.setResizeable(false);
		     this.MyInterface.getLayeredPane().add(f);
		     graphGantt GR= new graphGantt(MonKL.Logs.getHistoCPU(j),graphGantt.TypeGraph.CPU,MonKL.duration);

			f.getContentPane().add(GR);
			this.MyInterface.repaint();
			f.setTitle(MonKL.Logs.getCPUName(j));
			this.listegraphe.add(f);
			f.toFront();
			
		  }
		for (int k=0; k< MonKL.Logs.getNbreTask();k++){
			  InternalFrame f = new InternalFrame("InnerFrame ");
			  f.setBounds(10, 50+(k*10), 610, 160);  
			  f.setResizeable(false);
		     this.MyInterface.getLayeredPane().add(f);
		     graphGantt GR= new graphGantt(MonKL.Logs.getHistoTask(k),MonKL.Logs.getEvtTask(k),graphGantt.TypeGraph.TASK,MonKL.Logs.getEvtTaskDuration(k),MonKL.duration);
			f.getContentPane().add(GR);
			this.MyInterface.repaint();
			f.setTitle(MonKL.Logs.getTaskName(k));
	
			this.listegraphe.add(f);
			f.toFront();
		  }
	}
/**
 * @deprecated
 * @param filename2
 */
	/*public void save(String filename2) {
		this.MonKL.Logs.toXml(filename2);
		// TODO Auto-generated method stub
		
	}*/

/**
 *  Windonws info
 */
	public void showInfoGraph(JConsole console) {
		console.message = new StringBuffer(" ");
		if (this.isAlive()){
			for (int i =0; i < this.listegraphe.size() ; i ++) {
				console.message.append("\n");
				InternalFrame crt = (InternalFrame) this.listegraphe.get(i) ;
				console.message.append("num: " + i + " -name- " + crt.getInfo() + " ");
			}
		}
		
	}
	/**
	 * 
	 * @return frame selected
	 */
	public InternalFrame getSelectFrame(){
		 InternalFrame res = null;
		 
			 for (int i =0; i < this.listegraphe.size() ; i ++) {
				 InternalFrame crt = (InternalFrame) this.listegraphe.get(i) ;
			  
				 if (crt.isSelected()) {
					 res = crt;
				 }
			 }
		
		 
		 return res;
	}
	/**
	 * Close all window
	 * @param console
	 */
	public void closeall(JConsole console) {
		if (this.isAlive()){
			for (int i =0; i < this.listegraphe.size() ; i ++) {
			
				InternalFrame crt = (InternalFrame) this.listegraphe.get(i) ;
				
				crt.close();
				this.MyInterface.remove(crt);

			}	
		}
		this.listegraphe = new ArrayList();
		this.explorer = null;
		
		
	}
/**
 * save window n in filename (jpeg format)
 * @param num
 * @param filename2
 */
	public void saveFrameAsJepg(String num, String filename2) {
		
		int i = Integer.parseInt(num);
		InternalFrame crt = (InternalFrame) this.listegraphe.get(i) ;
		crt.validate();
		crt.repaint();

		BufferedImage outImage=new BufferedImage(crt.getWidth(),crt.getHeight(),BufferedImage.TYPE_3BYTE_BGR);
		File outFile=new File(filename2);
		     Graphics2D graphics=outImage.createGraphics();
		    
		     crt.paint(graphics);
		  try {
			         if (!ImageIO.write(outImage,"jpg",outFile))
			             System.out.println("Format d'??criture non pris en charge" );
			     } catch (Exception e) {
			         System.out.println("erreur dans l'enregistrement de l'image :" );
			         e.printStackTrace();
			     }

		

		
	}
	/**
	 * Zoom for gantt graph
	 * @param num
	 * @param pos
	 * @param zoom
	 */
	public void setPos(String num, String pos,String zoom) {
		int i = Integer.parseInt(num);
		int deb = Integer.parseInt(pos);
		InternalFrame crt = (InternalFrame) this.listegraphe.get(i) ;
		int z = Integer.parseInt(zoom);
		switch(z){
		case 0:
			crt.setTempPos(deb,graphGantt.ZOOM.zero);
			break;
		case 1:
			crt.setTempPos(deb,graphGantt.ZOOM.un);
			break;
		case 2:
			crt.setTempPos(deb,graphGantt.ZOOM.deux);
			break;
		case 3:
			crt.setTempPos(deb,graphGantt.ZOOM.trois);
			break;
		default:
			crt.setTempPos(deb,graphGantt.ZOOM.un);
				
		}
		
	}
	/**
	 * tanslation for gantt graph
	 * @param num
	 * @param pos
	 */
	public void setPos(String num, String pos) {
		int i = Integer.parseInt(num);
		int deb = Integer.parseInt(pos);
		InternalFrame crt = (InternalFrame) this.listegraphe.get(i) ;
		crt.setTempPos(deb,graphGantt.ZOOM.un);
		// TODO Auto-generated method stub
		
	}
/**
 * context of current simulation 
 * @param console
 */
	public void showctx(JConsole console) {
		console.message = new StringBuffer(" ");
		if (this.MonKL !=null){
		console.message.append(" \n Duration " + this.MonKL.duration + "\n");
		console.message.append(this.MonKL.getCtx());
		}
		
	}
/**
 * Quit programme
 */
	public void quit() {
		System.exit(0);
	
	}
	/**
	 * draw remaining execution time for task n
	 * @param num
	 * @return
	 */
	public boolean drawRETGraph(String num) {
		int i = Integer.parseInt(num);
		ArrayList Tasks = MonKL.Logs.getEvtTaskById(i);
		if (Tasks == null) return false;
		graphXY chart= new graphXY();
		chart.setYTile("RET in ms");
		chart.setLegend("RET ");
		  double pred=0;
		  
		for (int k = 0 ; k < Tasks.size();k++){
			TaskTrace t = (TaskTrace) Tasks.get(k);
			if (t.type.ordinal() == TaskTrace.Type.RET.ordinal()) {
				if (pred != t.RET){
					chart.addPoint(t.date,pred);
				}
				chart.addPoint(t.date,t.RET);
				pred=t.RET;
			}
			
		}
		chart.draw(); 
		InternalFrame f = new InternalFrame("InnerFrame ");
		 f.setBounds(10, 80, 400, 200);

		 f.getContentPane().add(chart);
		 this.MyInterface.getLayeredPane().add(f, BorderLayout.CENTER);
	     
	     this.MyInterface.repaint();
		 f.setTitle("RET for Task: "+MonKL.Logs.getTaskNameByid(i)); 
 		 this.listegraphe.add(f);f.toFront();
		 f.toFront();
		 this.MyInterface.repaint();
		 return true;
	}
	/**
	 * draw fixed remaining execution time for task n
	 * @param num
	 * @return
	 */
	/*public boolean drawFRETGraph(String num) {
		int i = Integer.parseInt(num);
		ArrayList Tasks = MonKL.Logs.getEvtTaskById(i);
		if (Tasks == null) return false;
		graphXY chart= new graphXY();
		chart.setYTile("FRET in ms");
		chart.setLegend("RET ");
		  double pred=0;
		  
		for (int k = 0 ; k < Tasks.size();k++){
			TaskTrace t = (TaskTrace) Tasks.get(k);
			if (t.type.ordinal() == TaskTrace.Type.RET.ordinal()) {
				//trace de releve de la valeur du RET
				if (pred != t.VRET){
					chart.addPoint(t.date,pred);
				}
				chart.addPoint(t.date,t.VRET);
				pred=t.VRET;
			}
			
		}
		chart.draw(); 
		InternalFrame f = new InternalFrame("InnerFrame ");
		 f.setBounds(10, 80, 400, 200);

		 f.getContentPane().add(chart);
		 this.MyInterface.getLayeredPane().add(f, BorderLayout.CENTER);

	     
	     this.MyInterface.repaint();
		 f.setTitle("F-RET for Task: "+MonKL.Logs.getTaskNameByid(i)); 
 		 this.listegraphe.add(f);f.toFront();
		 f.toFront();
		 this.MyInterface.repaint();
		 return true;
	}*/
	/**
	 * draw dvs graph for processor num
	 * @param num
	 * @return
	 */
	public boolean drawDVSGraph(String num) {
		int i = Integer.parseInt(num);
		String name = MonKL.Logs.getCPUNameById(i);
		if (name.compareToIgnoreCase("") == 0 ) return false;
		ArrayList DVS = MonKL.Logs.getDVSSet();
		graphXY chart= new graphXY();
		chart.setYTile("Frequencies HZ");
		chart.setLegend("DVS Change ");
		chart.setType(graphXY.type.DISC);

		  for (int k=0;k <  DVS.size();k++){
			  	
				DVSTrace T = (DVSTrace) DVS.get(k);
				if ((T.getCPU()).getId() != i ) continue; 

				chart.addPoint(T.date,T.getFreq());

				
		  }
		
		
		 chart.draw(); 
			InternalFrame f = new InternalFrame("InnerFrame ");
			 f.setBounds(10, 80, 400, 200);
			 f.getContentPane().add(chart);
			 this.MyInterface.getLayeredPane().add(f, BorderLayout.CENTER);
		     this.MyInterface.repaint();
		     f.toFront();
			 f.setTitle("CPU DVS : "+name);
	 		 this.listegraphe.add(f);f.toFront();
			 this.MyInterface.repaint();
			 return true;
		
		
		
	}
	/**
	 * draw load graph for processor P
	 * @param num
	 * @return
	 */
	public boolean drawLoadGraph(String num) {
		int i = Integer.parseInt(num);
		ArrayList cpuPC = MonKL.Logs.getHistoPowerbyId(i);
		if (cpuPC==null ) return false;
		graphXY chart= new graphXY();
		chart.setYTile("Load in %");
		chart.setLegend("Load ");
		  double pred=0;
		  double ld =0;
		  for (int k=0;k <  cpuPC.size();k++){
				powerTrace T = (powerTrace) cpuPC.get(k);
				ld = (((double)T.getLoad())/(double)(T.date+1)) * 100;
				chart.addPoint(T.date,ld);
			   
				
		  }
		    chart.draw(); 
			InternalFrame f = new InternalFrame("InnerFrame ");
			 f.setBounds(10, 80, 400, 200);
			 f.getContentPane().add(chart);
			 this.MyInterface.getLayeredPane().add(f, BorderLayout.CENTER);
		     this.MyInterface.repaint();
			 f.setTitle("CPU Load : "+MonKL.Logs.getCPUNameById(i)); 
	 		 this.listegraphe.add(f);f.toFront();
			 f.toFront();
			 this.MyInterface.repaint();
			 return true;
		
	}
	/**
	 * draw power graph for processor num
	 * @param num
	 * @return
	 */
	public boolean drawPowerGraph(String num) {
		int i = Integer.parseInt(num);
		ArrayList cpuPC = MonKL.Logs.getHistoPowerbyId(i);
		if ( cpuPC== null ) return false;

		graphXY chart= new graphXY();
		chart.setYTile("Power in W");
		chart.setLegend("Power ");
		  double pred=0;
		  for (int k=0;k <  cpuPC.size();k++){
				powerTrace T = (powerTrace) cpuPC.get(k);
				if (pred != T.power){
					chart.addPoint(T.date,pred);
				}
				chart.addPoint(T.date,T.power);

				pred=T.power;

				
		  }
 
		    chart.draw();
		
		InternalFrame f = new InternalFrame("InnerFrame ");
		 f.setBounds(10, 80, 400, 200);
		
		 f.getContentPane().add(chart);
		 this.MyInterface.getLayeredPane().add(f, BorderLayout.CENTER);
		
	     
	     this.MyInterface.repaint();
		 f.setTitle("CPU Power consumption  "+ MonKL.Logs.getCPUNameById(i));
 		 this.listegraphe.add(f);f.toFront();
		 f.toFront();
		 this.MyInterface.repaint();
		 return true;
		 
	}

/**
 * calcpower for procesor num
 * @param num
 * @param console
 * @return
 */

	public boolean calcPower(String num, JConsole console) {
		int i = Integer.parseInt(num);
		double res = 0;
		ArrayList cpuPC = MonKL.Logs.getHistoPowerbyId(i);
		if (cpuPC == null){
				console.message = new StringBuffer("Error ID");
				return false;
		}
		else{
		  for (int k=0;k <  cpuPC.size();k++){
				powerTrace T = (powerTrace) cpuPC.get(k);
				res = res + T.power;		
		  }
		res = res  *this.MonKL.precision / 1000; //unit?? de m??sure en ms
		double dur= (double)(MonKL.duration / 1000.0 *this.MonKL.precision);
		console.message = new StringBuffer("CPU Power consumption for "  + MonKL.Logs.getCPUNameById(i) +" " + String.format("\n %2.2f",res) + " J " + "\n duration " + dur + " s" +  String.format("\n %2.2f",(double)(res/dur)) + " W");
		}
		return true;  
	}

	public void saveFrameall() {
		for (int i =0; i < this.listegraphe.size() ; i ++) {
			InternalFrame crt = (InternalFrame) this.listegraphe.get(i);
			
			this.saveFrameAsJepg(i+"","/tmp/"+((crt.getTitle()).trim()).replace(" ","")+".jpg");
		}
		
	}
/**
 * show Task graph 
 * @param console
 */
	public void showDataGraph(JConsole console) {
		
		Vector name=new Vector();
		Vector matrice_adjacence = new Vector();
		GraphModel model = new DefaultGraphModel();
		this.MonKL.getTasksListeManager().dataSetFactory(name, matrice_adjacence);
		JData jdata = new JData(name,matrice_adjacence,model);
		InternalFrame f = new InternalFrame("InnerFrame ");
		f.setTitle("Task Graph ");
		JScrollPane defiler = new  JScrollPane(jdata);
		f.getContentPane().add(defiler);
		f.setBounds(10, 80, 400, 200);
		/**
		 *RICHARD TO STUDY may be a bug 
		 */
		
		 f.setEnabled(false) ;
		 /**
		  * 
		  */
		 f.toFront();
		this.MyInterface.getLayeredPane().add(f, BorderLayout.CENTER);
		this.listegraphe.add(f);
		 f.toFront();
		this.MyInterface.repaint();
	}
	/**
	 * show dvfs message (change frequency)
	 * @param deb
	 * @param fin
	 */
	public void showdvs(String deb, String fin) {
		int d = Integer.parseInt(deb);
		int fi = Integer.parseInt(fin);
		Vector enTete= new Vector();
		Vector res = new Vector();
		enTete.add("Date");
		enTete.add("Processeur");
		enTete.add("Task");
		enTete.add("Old dvsfactor");
		enTete.add("New dvsfactor");
		enTete.add("Old RET");
		enTete.add("New RET");
		ArrayList DVSSet = MonKL.Logs.getDVSSet();
		for (int j=0; j < DVSSet.size();j++){
			DVSTrace  m =(DVSTrace) DVSSet.get(j);
			if ( (m.date >= d ) && (m.date <= fi) ) { 
				res.add(m.getLigne());
			}
			
		}
		JTable tableMessage = new JTable(res,enTete);
		JScrollPane defiler = new  JScrollPane(tableMessage);
		tableMessage.setRowHeight(tableMessage.getRowHeight() + 10);
		
		InternalFrame f = new InternalFrame("InnerFrame ");
		f.getContentPane().add(defiler,BorderLayout.CENTER);
		f.setBounds(10, 80, 400, 200);
		f.setTitle("Messages DVS");
		f.toFront();
		this.MyInterface.getLayeredPane().add(f, BorderLayout.CENTER);
		this.listegraphe.add(f);f.toFront();
		this.MyInterface.repaint();
		
	}
	/**
	 * Show internel kernel message
	 * @param num
	 */
	public void showmess(String num) {
		// TODO Auto-generated method stub
		int i = Integer.parseInt(num);
		Vector enTete= new Vector();
		Vector res = new Vector();
		enTete.add("Date");
		enTete.add("Source");
		enTete.add("Destination");
		enTete.add("Type");
		enTete.add("Contexte");
		ArrayList messSet = this.MonKL.Logs.getmessSet();
		for (int j=0; j < messSet.size();j++){
			messageTrace  m =(messageTrace) messSet.get(j);
			if(i==m.getDate()) {//bonne date
				Vector Ligne = new Vector();
				Ligne.add(m.getDate()+"");
				Ligne.add(m.getSource());
				Ligne.add(m.getDestination());
				Ligne.add(m.getType());
				Ligne.add(m.getContexte());
				res.add(Ligne);
				
			}
			
		}
		//
		JTable tableMessage = new JTable(res,enTete);
		JScrollPane defiler = new  JScrollPane(tableMessage);
		tableMessage.setRowHeight(tableMessage.getRowHeight() + 10);
		
		InternalFrame f = new InternalFrame("InnerFrame ");
		f.getContentPane().add(defiler,BorderLayout.CENTER);
		f.setBounds(10, 80, 400, 200);
		f.setTitle("Messages at date " + i);

		this.MyInterface.getLayeredPane().add(f, BorderLayout.CENTER);
		this.listegraphe.add(f);		f.toFront();
		this.MyInterface.repaint();
		 
	}
/**
 * set zoom for XY graph
 * @param num
 * @param x1
 * @param x2
 */
	public void setZoom(String num, String x1, String x2) {
		int i = Integer.parseInt(num);
		int X1= Integer.parseInt(x1);
		int X2= Integer.parseInt(x2);
		InternalFrame crt = (InternalFrame) this.listegraphe.get(i) ;
		crt.setTempZoom(X1,X2);
		// TODO Auto-generated method stub
		
	}
/**
 * reset zoom for XY graph
 * @param num
 */
	public void setZoomAll(String num) {
		int i = Integer.parseInt(num);
		// TODO Auto-generated method stub
		InternalFrame crt = (InternalFrame) this.listegraphe.get(i) ;
		crt.setTempZoomAll();
	}
/**
 * test if window exist
 * @return
 */
	public boolean isAlive() {
		if (this.listegraphe==null) return false;
		else return true;
	}
 
	/**
	 * 
	 * @return all the window frame
	 */
	public ArrayList getListeFrame() {
		return this.listegraphe;
	}

	/**
	 * remove select frame 
	 * @param crt
	 */
	public void removeFrame(InternalFrame crt) {
		crt.close();
		if (crt.isExplorer()) this.explorer = null;
		
		this.listegraphe.remove(crt);
	

		
	}
	/**
	 * rearrange window
	 * @param mode
	 */

	public void reArrangeWindow(int mode) {
		 
		Dimension d = this.MyInterface.getSize(); ;
		Dimension dc = this.MyInterface.frameConsole.getSize();
		int x=30;
		int y=0;
		switch (mode){
		
		case 0: //save postion
			if (this.isAlive()) {
				Iterator I1 = this.listegraphe.iterator();
				while(I1.hasNext()){
					InternalFrame crt = (InternalFrame) I1.next() ;
					crt.oldRectangle = crt.getBounds();
					
				}
				
			}
			this.MyInterface.frameConsole.toFront();
			break;
			
		case 1: // restore position
			
		//	this.MyInterface.frameConsole.setBounds(10,d.height-30-dc.height, dc.width, dc.height);
			
			if (this.isAlive()) {
				Iterator I = this.listegraphe.iterator();
				while(I.hasNext()){
					InternalFrame crt = (InternalFrame) I.next() ;
					if (crt.oldRectangle!=null){
						crt.setBounds(crt.oldRectangle);
					}
				}
			}
			this.MyInterface.frameConsole.toFront();
			break;
				

		case 2: //Grouped 
			this.MyInterface.frameConsole.setBounds(10,d.height-dc.height-30, dc.width, dc.height);
			x=30;
			if (this.isAlive()) {
			Iterator I11 = this.listegraphe.iterator();
			int i=0;
			int j = 0;
			while(I11.hasNext()){
				
				InternalFrame crt = (InternalFrame) I11.next() ;
				Dimension df = crt.getSize();
				crt.setBounds(x,j*30,df.width,df.height);
				crt.toFront();
				i = i+30;
				j=j+1;
				if (i>(d.height-80)) { x = x + 100 ; i =0;j=0;}
				
			}
			}
			
			this.MyInterface.frameConsole.toFront();
		
			break;
		case 3: // all space
		    x=0;
			
		//	this.MyInterface.frameConsole.setBounds(0,0, dc.width, dc.height);
		//	x = x + dc.width;
			
			if (this.isAlive()){
				Iterator I111 = this.listegraphe.iterator();
				while(I111.hasNext()){
					InternalFrame crt = (InternalFrame) I111.next() ;
					Dimension df = crt.getSize();
					crt.setBounds(x,y,df.width,df.height);
					crt.toFront();
					x = x + df.width + 5 ;
					if (x>d.width) {
						x = 0;
						y = y + df.height+5;
					}
					if (y>d.height) {
						x = 30;
						y = 30;
					}
				}
			}
			this.MyInterface.frameConsole.toFront();
			break;
		case 4: //grouped by theme
			this.MyInterface.frameConsole.setBounds((int)(d.width*0.25-dc.width/2),(int)(d.height*0.75-dc.height/2), dc.width, dc.height);
			int x1=0,x2=0,x3=0;
			int y1=0,y2=0,y3=0;
			if (this.isAlive()){
				Iterator I = this.listegraphe.iterator();
				while(I.hasNext()){
					InternalFrame crt = (InternalFrame) I.next() ;
					Dimension df = crt.getSize();
					if (crt.isGantt()){
						crt.setBounds(x1+30,y1,df.width,df.height);
						y1=y1+30;
						
					}
					else if (crt.isXY()){
						crt.setBounds(x2+(d.width/2),y2,df.width,df.height);
						y2 =y2+30;
						
					}
					else {
						crt.setBounds(x3+(d.width/2),y3+(d.height/2),df.width,df.height);
						y3=y3+30;
					}
					
				}
			}
			this.MyInterface.frameConsole.toFront();
			break;
		}
	}
	
	InternalFrame explorer=null;
	
	/**
	 * create (if necessary ) and show explorer window
	 * @param console
	 */
	public void showExplorer(JConsole console) {
		if (isAlive()){
		if (explorer == null ) {
		// TODO Auto-generated method stub
			Explorer ex = new Explorer(MonKL,console);
		
			explorer  = new InternalFrame("InnerFrame ");
			explorer.explorer = true;
			explorer.getContentPane().add(ex,BorderLayout.CENTER);
			explorer.setBounds(10, 80, 400, 200);
			explorer.setTitle("Explorer");
			explorer.toFront();
			this.MyInterface.getLayeredPane().add(explorer, BorderLayout.CENTER);
			this.listegraphe.add(explorer);
			this.MyInterface.repaint();
		}
		else{
		
			if (explorer.isVisible()) explorer.setVisible(false);
			else explorer.setVisible(true);
			
		
		}
		explorer.toFront();
		}
		
	}
/**
 * Close window num
 * @param num
 */
	public void CloseWindow(String num) {
		int i = Integer.parseInt(num);
		InternalFrame crt = (InternalFrame) this.listegraphe.get(i) ;
		
		
		crt.close();
		
		this.MyInterface.remove(crt);
		this.listegraphe.remove(crt);
		
	}
/**
 * Show message penality beetween two date
 * @param deb
 * @param fin
 */
	public void showpenality(String deb, String fin) {
		int d = Integer.parseInt(deb);
		int fi = Integer.parseInt(fin);
		Vector enTete= new Vector();
		Vector res = new Vector();
		enTete.add("Date");
		enTete.add("Type");
		enTete.add("message");
		ArrayList penalitySet = MonKL.Logs.getpenalitySet();
		//System.out.println(penalitySet.size());
		for (int j=0; j < penalitySet.size();j++){
			penalityTrace  m =(penalityTrace) penalitySet.get(j);
			if ( (m.date >= d ) && (m.date <= fi) ) { 
				res.add(m.getLigne());
			}
			
		}
		JTable tableMessage = new JTable(res,enTete);
		JScrollPane defiler = new  JScrollPane(tableMessage);
		tableMessage.setRowHeight(tableMessage.getRowHeight() + 10);
		
		InternalFrame f = new InternalFrame("InnerFrame ");
		f.getContentPane().add(defiler,BorderLayout.CENTER);
		f.setBounds(10, 80, 400, 200);
		f.setTitle("Penalities for date " + deb + " to " + fin);

		this.MyInterface.getLayeredPane().add(f, BorderLayout.CENTER);
		this.listegraphe.add(f);		f.toFront();
		this.MyInterface.repaint();
		
	}
/**
 * create file report 
 * @param console
 * @param f
 * @param out
 * @return
 */
	public boolean processReport(JConsole console, String f, String out) {
		if (this.MonKL!=null){
			report r = new report(console,f,out);
			this.MonKL.setReport(r);
			boolean res = this.MonKL.getReport().process(console);
			return res;
			
		}
		return false;
	}
/**
 * draw a gantt graph (processor or task)
 * @param num
 * @return
 */
	public boolean drawGantt(String num) {
		
		InternalFrame f = new InternalFrame("InnerFrame ");
		int i = Integer.parseInt(num);
		this.MonKL.Logs.getNbreCpu();
		if (this.MonKL.isCPU(i)){
		
		  f.setBounds(100, 50,610, 160);
		 f.setResizeable(false);
	     this.MyInterface.getLayeredPane().add(f);
        graphGantt GR= new graphGantt(MonKL.Logs.getHistoCPUById(i),graphGantt.TypeGraph.CPU,MonKL.duration);

		f.getContentPane().add(GR);
		this.MyInterface.repaint();
		f.setTitle(MonKL.Logs.getCPUNameById(i));
		this.listegraphe.add(f);
		f.toFront();
		return true;
		}
		if (this.MonKL.isTask(i)){
			
			  f.setBounds(100, 50,610, 160);
			 f.setResizeable(false);
		     this.MyInterface.getLayeredPane().add(f);
  
//	        graphGantt GR= new graphGantt(MonKL.Logs.getHistoTaskById(i),graphGantt.TypeGraph.TASK,MonKL.duration);
		//     graphGantt GR= new graphGantt(MonKL.Logs.getHistoTaskById(i),graphGantt.TypeGraph.TASK,MonKL.duration);
		     graphGantt GR= new graphGantt(MonKL.Logs.getHistoTaskById(i),MonKL.Logs.getEvtTaskById(i),graphGantt.TypeGraph.TASK,MonKL.Logs.getEvtTaskDurationById(i),MonKL.duration);


			f.getContentPane().add(GR);
			this.MyInterface.repaint();
			f.setTitle(MonKL.Logs.getTaskNameByid(i));
			this.listegraphe.add(f);
			f.toFront();
			return true;
			}

		return false;
	}
	
	/**
	 * Draw AET Graph for task I
	 * @param num
	 * @return
	 */
public boolean drawAETGraph(String num) {
	// TODO Auto-generated method stub

	int i = Integer.parseInt(num);
	

	graphXY chart= new graphXY();
	chart.setYTile("AET in ms");
	chart.setLegend("AET ");
	
	Task T = this.MonKL.getTasksListeManager().getTaskbyId(i);
	if (T==null) return false;
	classSonde sonde = T.getMySonde();
	 for (int j = 0; j < sonde.getNombreligne();j++){
			Vector vec = sonde.getligne(j);
			if (vec.size()>1){
				chart.addPoint(new Integer((String)vec.get(0)),new Integer ((String)vec.get(3)));
			}
		}


	
	
		chart.draw(); 
	InternalFrame f = new InternalFrame("InnerFrame ");
	 f.setBounds(10, 80, 400, 200);

	 f.getContentPane().add(chart);
	 this.MyInterface.getLayeredPane().add(f, BorderLayout.CENTER);
     
     this.MyInterface.repaint();
	 f.setTitle("AET for Task: "+MonKL.Logs.getTaskNameByid(i)); 
		 this.listegraphe.add(f);f.toFront();
	 f.toFront();
	 this.MyInterface.repaint();
	 return true;
	
	
	

}
	public boolean processL(String filename2,JConsole console) {
		// TODO Auto-generated method stub
		//System.out.println(filename2);
	//	generator evaluation = new generator(filename2,console);
		//return evaluation.proceed();
		return false;
	}
	/**
	 * 
	 */
	public void freeAll() {
		this.MonKL.freeAll();
		this.MonKL = null;
		
		// TODO Auto-generated method stub
		
	}
	public boolean getResponseTime(String num, JConsole jConsole) {
		
		int i = Integer.parseInt(num);
		double res = 0;
		ArrayList cpuPC = MonKL.Logs.getHistoPowerbyId(i);
		Task T = MonKL.getTasksListeManager().getTaskbyId(i);
		if (T == null){
				jConsole.message = new StringBuffer("Error ID");
				return false;
		}
		else{
		
		jConsole.message = new StringBuffer(T.getResponseTime());
		}
		return true;  

		// TODO Auto-generated method stub
	
	}










}

